package com.medicine.hbp.fragment.x;

import java.util.List;

import org.kymjs.kjframe.http.HttpParams;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.medicine.hbp.adapter.ClassificationDetailAdapter;
import com.medicine.hbp.adapter.FragmentAdapter03;
import com.medicine.hbp.adapter.FragmentAdapter04;
import com.medicine.hbp.adapter.FragmentGrid04;
import com.medicine.hbp.adapter.FragmentList04;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.mob.findLaberAllByParentIdVo;
import com.medicine.hbp.mob.findParentAllByTypeVo;
import com.medicine.hbp.mob.findParentinfoVo;
import com.medicine.hbp.sp.DadosSp;
import com.medicine.hbp.sp.InterestSp;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.sp.UtilsSp;
import com.medicine.hbp.ui.IntegralActivity;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

/**
 * 收藏界面
 */
public class Fragment04 extends Fragment implements OnItemClickListener,
		IXListViewListener {
	private View view;
	private XListView xlistview;
	private ClassificationDetailAdapter adapter;
	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	private List<artGroupVo> artGroupVos;
	private Gson gson;
	private TextView no_login;
	private ImageView NoNetWork;

	@Override
	public android.view.View onCreateView(LayoutInflater inflater,
										  ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_04, container, false);
		xlistview = (XListView) view.findViewById(R.id.xListView);
		no_login = (TextView) view.findViewById(R.id.no_login);
		NoNetWork = (ImageView) view.findViewById(R.id.image_no_wifi);
		initView();
		return view;
	}

	private void initView() {
		gson = new Gson();
		adapter = new ClassificationDetailAdapter(getActivity());
		xlistview.setAdapter(adapter);
		xlistview.setOnItemClickListener(this);
		xlistview.setXListViewListener(this);
		no_login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				StartIntent.startLogin(getActivity());
			}
		});
//		DadosSp.setAdapter(getActivity(),LoginSp.getUserId(getActivity()) + "", adapter);
		NoNetWork.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (NetWorkUtils.getAPNType(getActivity()) == 0) {
					xlistview.setVisibility(View.GONE);
					NoNetWork.setVisibility(View.VISIBLE);
					ToastUtil.showShort(getActivity(), "断网啦！");
				} else {
					xlistview.setVisibility(View.VISIBLE);
					NoNetWork.setVisibility(View.GONE);
					httpLaberGroupList();
				}

			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		if (TextUtils.isEmpty(LoginSp.getToken(getActivity()))) {
			no_login.setVisibility(View.VISIBLE);
			no_login.setText("请先登录再进行收藏");
			xlistview.setVisibility(View.GONE);
		} else {
			if (NetWorkUtils.getAPNType(getActivity()) == 0) {
				xlistview.setVisibility(View.GONE);
				NoNetWork.setVisibility(View.VISIBLE);
				no_login.setVisibility(View.GONE);
			} else {
				xlistview.setVisibility(View.VISIBLE);
				NoNetWork.setVisibility(View.GONE);
				no_login.setVisibility(View.GONE);
				httpLaberGroupList();
			}
		}

	}

	/**
	 * 首页标签组获取
	 */
	private void httpLaberGroupList() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(getActivity(), HttpInterface.USER_COLLECT_LIST,
				params, new AppHttpCallBack<artGroupListVo>(
						artGroupListVo.class, getActivity()) {
					@Override
					public void onSuccess(artGroupListVo result) {
						boolean hasMore = true;
						artGroupVos = result.artInfo;
						if (refresh) {
							if (artGroupVos.size() > 0) {
								DadosSp.setClassificationDetail(getActivity(),
										LoginSp.getUserId(getActivity()) + "",
										gson.toJson(result));
								adapter.changeData(artGroupVos);
							} else {
								no_login.setText("^_^快来收藏您喜欢的方法吧！");
								no_login.setVisibility(View.VISIBLE);
								xlistview.setVisibility(View.GONE);
							}
						} else {
							if (UtilsSp.getIsYm(getActivity())) {
								artGroupVo vo = new artGroupVo();
								vo.isbanner = true;
								artGroupVos.add(vo);
							}
							adapter.addItems(artGroupVos);
							adapter.notifyDataSetChanged();
						}
						if (result.artInfo.size() < pageSize) {
							hasMore = false;
						}
						xlistview.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System
								.currentTimeMillis()));
					}
				});

	}

	// List<findParentinfoVo> info;
	// public void httplist() {
	// HttpParams params = new HttpParams();
	// params.put("laberParentType", "2");
	// AppHttps.HttpPost(getActivity(), HttpInterface.FIND_PARENT_ALL_BY_TYPE,
	// params,
	// new
	// AppHttpCallBack<findParentAllByTypeVo>(findParentAllByTypeVo.class,getActivity())
	// {
	//
	// @Override
	// public void onSuccess(findParentAllByTypeVo result) {
	// info=result.info;
	// adapterList.setItems(result.info);
	// adapterList.notifyDataSetChanged();
	// httpGrid(result.info.get(0).laberParentId);
	// }
	// @Override
	// public void onFailures(int errorCode, String errorMsg) {
	// ToastUtil.showShort(getActivity(), errorMsg);
	// }
	// });
	// }
	//
	// public void httpGrid(int id ) {
	// HttpParams params = new HttpParams();
	// params.put("laberParentId", id );
	// AppHttps.HttpPost(getActivity(),
	// HttpInterface.FIND_LABER_ALL_BY_PARENTID, params,
	// new
	// AppHttpCallBack<findLaberAllByParentIdVo>(findLaberAllByParentIdVo.class,getActivity())
	// {
	//
	// @Override
	// public void onSuccess(findLaberAllByParentIdVo result) {
	// adapterGrid.setItems(result.info);
	// adapterGrid.notifyDataSetChanged();
	// }
	// @Override
	// public void onFailures(int errorCode, String errorMsg) {
	// ToastUtil.showShort(getActivity(), errorMsg);
	// }
	// });
	// }

	@Override
	public void onRefresh() {
		page = 1;
		refresh = true;
		xlistview.setPullLoadEnable(false);
		httpLaberGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpLaberGroupList();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, android.view.View arg1,
							int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

}